package top.andnux.weather.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import top.andnux.weather.R;
import top.andnux.weather.activity.DetailsActivity;
import top.andnux.weather.model.MainModel;

public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.ViewHolder> {

    private final Context context;
    private final List<MainModel.ResultDTO.FutureDTO> list;
    private String city;

    public void setCity(String city) {
        this.city = city;
    }

    public String getCity() {
        return city;
    }

    public WeatherAdapter(Context context, List<MainModel.ResultDTO.FutureDTO> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public WeatherAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_week, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull WeatherAdapter.ViewHolder holder, int position) {
        MainModel.ResultDTO.FutureDTO item = list.get(position);
        holder.date.setText(item.getDate());
        holder.weather.setText(item.getWeather());
        holder.temperature.setText(item.getTemperature());
        holder.itemView.setOnClickListener(v -> {
            DetailsActivity.start(context, item, city);
        });
        String weather = item.getWeather();
        if (weather.contains("晴")) {
            holder.imageView.setImageResource(R.mipmap.qingtian);
        } else if (weather.contains("雪")) {
            holder.imageView.setImageResource(R.mipmap.zhenxue);
        } else if (weather.contains("雨")) {
            holder.imageView.setImageResource(R.mipmap.dayu);
        } else if (weather.contains("风")) {
            holder.imageView.setImageResource(R.mipmap.dafeng);
        } else if (weather.contains("雾")) {
            holder.imageView.setImageResource(R.mipmap.duoyunyewan);
        } else {
            holder.imageView.setImageResource(R.mipmap.yintian);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        protected ImageView imageView;
        protected TextView date, weather, temperature;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            date = itemView.findViewById(R.id.date);
            weather = itemView.findViewById(R.id.weather);
            temperature = itemView.findViewById(R.id.temperature);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}
